﻿using System.Linq;
using Mono.Cecil;

namespace MethodCaching.Weaving.Extensions
{
    /// <summary>
    /// Extends the <see cref="MethodDefinition"/> class.
    /// </summary>
    public static class MethodDefinitionExtensions
    {
        /// <summary>
        /// Identifies whether one more attributes of the specified <paramref name="attributeType"/> 
        /// is applied to the target <paramref name="methodDefintion"/>
        /// </summary>
        /// <param name="methodDefintion">The target <see cref="TypeDefinition"/>.</param>
        /// <param name="attributeType">The type of custom attribute to search for.</param>
        /// <returns>true if on or more attributes of the specified <paramref name="attributeType"/> 
        /// is applied to the target <paramref name="methodDefintion"/>, otherwise, false.</returns>
        public static bool IsDefined(this MethodDefinition methodDefintion, TypeReference attributeType)
        {
            return methodDefintion.CustomAttributes.Any(ca => ca.AttributeType.FullName == attributeType.FullName);
        }
    }
}
